<template>
	<a-modal :title="title" :width="1400" :visible="visible" :confirmLoading="confirmLoading" :footer="null"
		@ok="handleSubmit" @cancel="handleCancel">
		<a-card :bordered="false">
			<div class="table-page-search-wrapper">
				<a-form layout="inline">
					<a-row :gutter="48">

						<a-col :md="8" :sm="24" v-if="false">
							<a-form-item label="订单ID">
								<a-input v-model="queryParam.orderId" placeholder="请输入订单ID" />
							</a-form-item>
						</a-col>
					</a-row>
				</a-form>
			</div>

			<div class="table-operator" v-if="!disabled && hasPerm('emsOrderMaterial:addOverseasProduct')">
				<a-button v-if="!isOverseasStore" icon="plus" type="primary" @click="$refs.productAddForm.add(emsOrder)">新增产品
				</a-button>
				<a-button v-else icon="plus" type="primary" @click="$refs.overseasProductAddForm.add(emsOrder)">添加海外仓产品
				</a-button>
			</div>

			<s-table ref="table" size="default" :columns="columns" :data="loadData" :alert="true"
				:rowKey="(record) => record.code"
				:rowSelection="null && { selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" :showPagination="false">

				<span slot="action" slot-scope="text, record" v-if="!disabled">
					<a @click="$refs.productBaseEditForm.edit(record)">基础编辑</a>
					<a-divider type="vertical" />
					<a @click="$refs.productBomEditForm.edit(record)">BOM编辑</a>
					<a-divider type="vertical" v-if="hasPerm('emsOrderMaterial:delete')" />
					<a-popconfirm placement="topRight" title="确认删除？" @confirm="() => del(record)"
						v-if="hasPerm('emsOrderMaterial:delete')">
						<a>删除</a>
					</a-popconfirm>
				</span>

				<!-- <span slot="actionInProduction" slot-scope="text, record">
          <a @click="$refs.productDetailForm.add(record)">产品明细</a>
        </span> -->
				<span slot="remark" slot-scope="text, record">
					<a-tooltip placement="topLeft" :title="record.material.remark">
						{{ record.material.remark }}
					</a-tooltip>
				</span>


			</s-table>
		</a-card>

		<product-base-edit-form ref="productBaseEditForm" @ok="handleOk" />
		<product-add-form ref="productAddForm" @ok="handleOk" />
		<product-bom-edit-form ref="productBomEditForm" @ok="handleOk" />
		<product-detail-form ref="productDetailForm" @ok="handleOk" />
		<overseas-product-add-form ref="overseasProductAddForm" @ok="handleOk" />
	</a-modal>
</template>

<script>
import { STable } from '@/components'
import { page, del, add } from '@/api/modular/main/emsordermaterial/emsOrderMaterialManage'
import productBaseEditForm from './productBaseEditForm'
import productAddForm from './productAddForm'
import productBomEditForm from './productBomEditForm'
import productDetailForm from './productDetailForm'
import overseasProductAddForm from './overseasProductAddForm'

export default {
	components: {
		STable,
		productBaseEditForm,
		productAddForm,
		productBomEditForm,
		productDetailForm,
		overseasProductAddForm
	},
	data() {
		return {
			advanced: false, // 高级搜索 展开/关闭
			queryParam: {},  // 查询参数
			columns: [
				{
					title: '产品编号',
					dataIndex: 'materialCode'
				},
				{
					title: '产品别名',
					dataIndex: 'materialName'
				},
				{
					title: '需求数',
					dataIndex: 'number'
				},
				{
					title: '序号',
					width: '100px',
					dataIndex: 'extend1'
				},
				{
					title: '软件版本号',
					dataIndex: 'extend2'
				},
				{
					title: '更新时间',
					dataIndex: 'updateTime'
				},
				{
					title: '备注',
					dataIndex: 'remark',
					ellipsis: true,
					scopedSlots: { customRender: 'remark' }
				},
				{
					title: '操作',
					dataIndex: 'action',
					width: '310px',
					scopedSlots: { customRender: 'action' }
				}
			],
			// 加载数据方法 必须为 Promise 对象
			loadData: parameter => {
				this.queryParam.orderId = this.emsOrder.orderId
				this.queryParam.sourceOrderMaterialId = this.emsOrder.sourceOrderMaterialId
				this.queryParam.materialType = this.materialType
				this.queryParam.pageSize = -1
				return page(Object.assign(parameter, this.queryParam)).then((res) => {
					res.data.rows.forEach(value => {
						value.thisCompleteNumber = 0
					})
					return res.data
				})
			},
			selectedRowKeys: [],
			selectedRows: [],
			defaultExpandedKeys: [],
			visible: false,
			confirmLoading: false,
			emsOrder: null,
			title: "",
			disabled: false,
		}
	},
	computed: {
		materialType() {
			return this.queryParam.materialType = this.emsOrder?.isOverseasStore == 'N' ? this.$businessEnum.MATERIAL_TYPE_ENUM.product.id :
				this.$businessEnum.MATERIAL_TYPE_ENUM.overseasProduct.id
		},
		isOverseasStore() {
			return this.materialType == this.$businessEnum.MATERIAL_TYPE_ENUM.overseasProduct.id
		}
	},
	methods: {
		show(record) {
			this.visible = true
			this.disabled = false;
			this.title = `产品列表（${record.orderNumber}）`
			this.emsOrder = record
			setTimeout(() => {
				this.handleOk()
				this.$refs.table.clearSelected()
			}, 100)
		},
		readonly(record) {
			this.show(record);
			this.columns.splice(this.columns.length - 1, this.columns.length)
			this.disabled = true;
		},
		del(record) {
			del(record).then((res) => {
				if (res.success) {
					this.$message.success('删除成功')
					this.$refs.table.refresh()
				} else {
					this.$message.error('删除失败：' + res.message)
				}
			}).catch((err) => {
				this.$message.error('删除错误：' + err.message)
			})
		},
		toggleAdvanced() {
			this.advanced = !this.advanced
		},
		handleOk() {
			this.$refs.table.refresh()
		},
		onSelectChange(selectedRowKeys, selectedRows) {
			this.selectedRowKeys = selectedRowKeys
			this.selectedRows = selectedRows
		},
		handleSubmit() {
		},
		handleCancel() {
			this.visible = false
		},
	}
}
</script>